package com.momo.configuration.miniapp;

import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import lombok.Getter;
import lombok.Setter;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Kagome on 2019/5/22.
 */
@Setter
@Getter
public class WxMpSpringRedisConfigStorage extends WxMaInMemoryConfig {
    protected volatile String accessTokenKey;

    protected volatile String jsapiTicketKey;

    //微信卡券的ticket单独缓存
    protected volatile String cardApiTicketKey;

    private final static String COMPONENT_ACCESS_TOKEN_KEY = "miniapp_access_token:";
    private final static String JSAPI_TICKET_KEY = "miniapp_jsapi_ticket:";
    private final static String CARD_API_TICKET_KEY = "miniapp_card_api_ticket:";


    private final RedisTemplate<String, String> redisTemplateString;

    public WxMpSpringRedisConfigStorage(RedisTemplate<String, String> redisTemplateString) {
        this.redisTemplateString = redisTemplateString;
    }

    @Override
    public String getAccessToken() {
        return redisTemplateString.opsForValue().get(this.accessTokenKey);
    }

    @Override
    public void setAppid(String appid) {
        super.setAppid(appid);
        accessTokenKey = COMPONENT_ACCESS_TOKEN_KEY.concat(appid);
        this.jsapiTicketKey = JSAPI_TICKET_KEY.concat(appid);
        this.cardApiTicket = CARD_API_TICKET_KEY.concat(appid);
    }

    @Override
    public boolean isAccessTokenExpired() {
        return redisTemplateString.getExpire(this.accessTokenKey) < 2;
    }

    @Override
    public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
        redisTemplateString.opsForValue().set(this.accessTokenKey, accessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
    }

    @Override
    public String getJsapiTicket() {
        return redisTemplateString.opsForValue().get(this.jsapiTicketKey);
    }

    @Override
    public boolean isJsapiTicketExpired() {
        return redisTemplateString.getExpire(this.jsapiTicketKey) < 2;
    }

    @Override
    public void expireJsapiTicket() {
        redisTemplateString.expire(this.jsapiTicketKey, 0, TimeUnit.SECONDS);
    }

    @Override
    public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
        // 预留200秒的时间
        redisTemplateString.opsForValue().set(this.jsapiTicket, jsapiTicket, expiresInSeconds - 200);
    }

    @Override
    public String getCardApiTicket() {
        return redisTemplateString.opsForValue().get(this.jsapiTicket);
    }

    @Override
    public boolean isCardApiTicketExpired() {
        return redisTemplateString.getExpire(this.cardApiTicketKey) < 2;
    }

    @Override
    public void expireCardApiTicket() {
        redisTemplateString.expire(this.cardApiTicketKey, 0, TimeUnit.SECONDS);
    }

    @Override
    public void updateCardApiTicket(String cardApiTicket, int expiresInSeconds) {
        // 预留200秒的时间
        redisTemplateString.opsForValue().set(this.cardApiTicketKey, cardApiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
    }
    @Override
    public void expireAccessToken() {
        redisTemplateString.expire(this.accessTokenKey,0,TimeUnit.SECONDS);
    }
}
